package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;

@RestController
@Api(tags = "报表统计")
@RequestMapping("/admin/report")
public class ReportController {

    @Autowired
    ReportService reportService;

    /**
     * 销售额统计
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation("销售额统计")
    @GetMapping("/turnoverStatistics")
    public Result turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        TurnoverReportVO reportVO = reportService.turnoverStatistics(begin,end);
        return Result.success(reportVO);
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation("用户统计")
    @GetMapping("/userStatistics")
    public Result userStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ){
        UserReportVO reportVO = reportService.userStatistic(begin,end);
        return Result.success(reportVO);
    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation("订单统计")
    @GetMapping("/ordersStatistics")
    public Result ordersStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ){
        OrderReportVO reportVO = reportService.ordersStatistics(begin,end);
        return Result.success(reportVO);
    }

    /**
     * 销量top10
     * @param begin
     * @param end
     * @return
     */
    @ApiOperation("销量top10")
    @GetMapping("/top10")
    public Result topStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ){
        SalesTop10ReportVO reportVO = reportService.topStatistics(begin,end);
        return Result.success(reportVO);
    }

    /**
     * 导出excel文件
     */
    @ApiOperation("导出excel文件")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {

        reportService.export(response);

    }
}
